﻿#include "LineChartCoordinateSettingData.h"
#include <QDomElement>
#include <QDomDocument>
#include <QDomAttr>

namespace pst
{
    LineChartCoordinateSettingData::LineChartCoordinateSettingData(QObject* parent)
        : QObject(parent)
        , m_xAxisDisplay(new Style2D_Axis(this))
        , m_xAxisRange(new Style2D_AxisRange(this))
        , m_yAxisDisplay(new Style2D_Axis(this))
        , m_yAxisRange(new Style2D_AxisRange(this))
        , m_grid(new Style2D_GridLine(this))
        , m_title(new Style2D_Title(this))
        , m_legend(new Style2D_Legend(this))
        //, m_dataProperty(new Style2D_DataProperty)
    {
    }

    LineChartCoordinateSettingData::~LineChartCoordinateSettingData()
    {
        //if (m_xAxisDisplay)
        //{
        //    delete m_xAxisDisplay;
        //    m_xAxisDisplay = nullptr;
        //}
        //if (m_xAxisRange)
        //{
        //    delete m_xAxisRange;
        //    m_xAxisRange = nullptr;
        //}
        //if (m_yAxisDisplay)
        //{
        //    delete m_yAxisDisplay;
        //    m_yAxisDisplay = nullptr;
        //}
        //if (m_yAxisRange)
        //{
        //    delete m_yAxisRange;
        //    m_yAxisRange = nullptr;
        //}
        //if (m_grid)
        //{
        //    delete m_grid;
        //    m_grid = nullptr;
        //}
        //if (m_title)
        //{
        //    delete m_title;
        //    m_title = nullptr;
        //}
        //if (m_legend)
        //{
        //    delete m_legend;
        //    m_legend = nullptr;
        //}
        //if (m_dataProperty)
        //{
        //    delete m_dataProperty;
        //    m_dataProperty = nullptr;
        //}
    }


    void LineChartCoordinateSettingData::setXAxisDisplay(Style2D_Axis* data)
    {
        m_xAxisDisplay = data;
    }

    Style2D_Axis* LineChartCoordinateSettingData::getXAxisDisplay() const
    {
        return m_xAxisDisplay;
    }

    void LineChartCoordinateSettingData::setXAxisRange(Style2D_AxisRange* data)
    {
        m_xAxisRange = data;
    }

    Style2D_AxisRange* LineChartCoordinateSettingData::getXAxisRange() const
    {
        return m_xAxisRange;
    }

    void LineChartCoordinateSettingData::setYAxisDisplay(Style2D_Axis* data)
    {
        m_yAxisDisplay = data;
    }

    Style2D_Axis* LineChartCoordinateSettingData::getYAxisDisplay() const
    {
        return m_yAxisDisplay;
    }

    void LineChartCoordinateSettingData::setYAxisRange(Style2D_AxisRange* data)
    {
        m_yAxisRange = data;
    }

    Style2D_AxisRange* LineChartCoordinateSettingData::getYAxisRange() const
    {
        return m_yAxisRange;
    }

    void LineChartCoordinateSettingData::setGrid(Style2D_GridLine* data)
    {
        m_grid = data;
    }
    Style2D_GridLine* LineChartCoordinateSettingData::getGrid() const
    {
        return m_grid;
    }

    void LineChartCoordinateSettingData::setTitle(Style2D_Title* data)
    {
        m_title = data;
    }

    Style2D_Title* LineChartCoordinateSettingData::getTitle() const
    {
        return m_title;
    }

    void LineChartCoordinateSettingData::setLegend(Style2D_Legend* data)
    {
        m_legend = data;
    }

    Style2D_Legend* LineChartCoordinateSettingData::getLegend() const
    {
        return m_legend;
    }

    //void LineChartCoordinateSettingData::setDataProperty(Style2D_DataProperty* data)
    //{
    //    m_dataProperty = data;
    //}

    //Style2D_DataProperty* LineChartCoordinateSettingData::getDataProperty() const
    //{
    //    return m_dataProperty;
    //}

    //void LineChartCoordinateSettingData::copyData(const LineChartCoordinateSettingData* data)
    //{
    //    if (this == data || data == nullptr) return;
    //    this->m_xAxisDisplay->copyData(data->m_xAxisDisplay);
    //    this->m_xAxisRange->copyData(data->m_xAxisRange);
    //    this->m_yAxisDisplay->copyData(data->m_yAxisDisplay);
    //    this->m_yAxisRange->copyData(data->m_yAxisRange);
    //    this->m_grid->copyData(data->m_grid);
    //    this->m_title->copyData(data->m_title);
    //    this->m_legend->copyData(data->m_legend);
    //    this->m_dataProperty->copyData(data->m_dataProperty);
    //}


    QDomElement& LineChartCoordinateSettingData::writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso)
    {
        QDomElement _LineChartCoordinateSettingDataRoot = doc->createElement("LineChartCoordinateSettingData");
        QDomAttr idattr = doc->createAttribute("ID");
        idattr.setValue(QString::number(1));
        _LineChartCoordinateSettingDataRoot.setAttributeNode(idattr);

        m_xAxisDisplay->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_xAxisRange->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_yAxisDisplay->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_yAxisRange->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_grid->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_title->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);
        m_legend->writeToProjectFile(doc, &_LineChartCoordinateSettingDataRoot, isdiso);

        element->appendChild(_LineChartCoordinateSettingDataRoot);

        return _LineChartCoordinateSettingDataRoot;
    }

    void LineChartCoordinateSettingData::readFromProjectFile(QDomElement* element,bool isdiso)
    {
        int id = element->attribute("ID").toInt();
        auto _AxisDisplayEle = element->elementsByTagName("Style2D_Axis");
        int displayCount = _AxisDisplayEle.count();//值为2
        auto _AxisRangeEle = element->elementsByTagName("Style2D_AxisRange");
        int rangeCount = _AxisRangeEle.count();//值为2
        auto _gridEle = element->elementsByTagName("Style2D_GridLine");
        auto _titleEle = element->elementsByTagName("Style2D_Title");
        auto _legendEle = element->elementsByTagName("Style2D_Legend");
        
        m_xAxisDisplay->readFromProjectFile(&_AxisDisplayEle.at(0).toElement(), isdiso);
        m_xAxisRange->readFromProjectFile(&_AxisRangeEle.at(0).toElement(), isdiso);
        m_yAxisDisplay->readFromProjectFile(&_AxisDisplayEle.at(1).toElement().toElement(), isdiso);
        m_yAxisRange->readFromProjectFile(&_AxisRangeEle.at(1).toElement(), isdiso);
        m_grid->readFromProjectFile(&_gridEle.at(0).toElement(), isdiso);
        m_title->readFromProjectFile(&_titleEle.at(0).toElement(), isdiso);
        m_legend->readFromProjectFile(&_legendEle.at(0).toElement(), isdiso);

    }


}
